<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="toggle" label-width="68px">
      <el-form-item label="歌名" prop="name">
        <el-input
          v-model="name"
          placeholder="请输入您要查询的歌名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <div class="right-toolbar">
        <el-tooltip class="item" effect="dark" :content="toggle?'隐藏搜索':'显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="toggle=!toggle"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getList"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table :data="songList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="id" width="80" sortable/>
      <el-table-column label="歌名" align="center" prop="name"/>
      <el-table-column label="歌手" align="center" prop="singer"/>
      <el-table-column label="热度" align="center" prop="hot" sortable>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hot==='是'">热曲</el-tag>
          <el-tag type="success" v-if="scope.row.hot==='否'">一般</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      layout="->,total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" label-width="0">
        <el-form-item label="" prop="songs">
          <el-table :data="userList2" @selection-change="handleSelectionChange2">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" align="center" prop="id" width="80" sortable/>
            <el-table-column label="歌名" align="center" prop="name"/>
            <el-table-column label="歌手" align="center" prop="singer"/>
            <el-table-column label="热度" align="center" prop="hot" sortable>
              <template slot-scope="scope">
                <el-tag v-if="scope.row.hot==='是'">热曲</el-tag>
                <el-tag type="success" v-if="scope.row.hot==='否'">一般</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getMusicList} from "@/api/song";
import {addMenuSongs, deleteMenuSongs, getMenuSongsList} from "@/api/menu";

const BASE_API = process.env.VUE_APP_BASE_API
const UPLOAD_API = BASE_API + 'music/file/upload'
export default {
  name: "User",
  data() {
    return {
      base_api: BASE_API,
      uploadApi: UPLOAD_API,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids2: [],
      // 非单个禁用
      single2: true,
      // 非多个禁用
      multiple2: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      songList: [],
      userList2: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuId: null,
        songId: null
      },
      name: null,
      toggle: true,
      id: Number(this.$route.params.id)
    }
  },
  created() {
    this.getList();
    this.queryParams.menuId = this.id
  },
  methods: {
    async getList() {
      if (this.name !== null && this.name !== '') {
        await getMusicList({name: this.name}).then(res => {
          if (res.data.list.length !== 0)
            this.queryParams.songId = res.data.list[0].id;
          else
            this.queryParams.songId = 0
        }).catch(err => {
          console.log(err);
        })
      }
      getMenuSongsList(this.queryParams).then(res => {
        this.songList = res.data.list[0].songList
        this.queryParams.songId = null
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null
      };
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.$refs.form.clearValidate()
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        songId: null,
        menuId: this.id
      }
      this.name = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelectionChange2(selection) {
      this.ids2 = selection.map(item => item.id)
      this.single2 = selection.length !== 1
      this.multiple2 = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加歌曲";
      getMusicList().then(res => {
        this.userList2 = res.data.list
      })
    },
    /** 提交按钮 */
    submitForm() {
      addMenuSongs(this.ids2, this.id).then(() => {
        this.$message.success("新增成功")
        this.open = false;
        this.getList();
        this.reset();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const that = this
      this.$confirm('是否确认删除序号为"' + ids + '"的数据项？').then(function () {
        return deleteMenuSongs(ids, that.id);
      }).then(() => {
        this.getList();
        this.$message("删除成功");
      }).catch((err) => {
        console.log(err)
      });
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
      console.log(`当前页: ${val}`);
    },
  }
}
</script>
<style scoped lang="scss">

$size: 150px;
$color: #DCDFE6;

.avatar-uploader {
  width: $size;
  height: $size;
  border: 1px dashed $color;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  font-size: 28px;
  color: rgba(96, 98, 102);
}

.img {
  margin-top: 10px;
  width: 100%;
  height: 100%;
  border-radius: 5px;
}

.right-toolbar {
  float: right;
}

</style>
